package com.zhc.dejingapi.utils.qing;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhc.dejingapi.entity.QingAppfieldsEntity;
import com.zhc.dejingapi.mapper.QingAppfieldsMapper;
import com.zhc.dejingapi.mapper.RealtimeMapper;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataUserModel;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class UploadUtils {

    private static final Logger log = LoggerFactory.getLogger(ProductUnitUploadUtils.class);

    @Value("${spring.qinglfowapi.useremail}")
    private String email;
    @Resource
    private QingflowApiInfo qingflowApiInfo;
    @Resource
    private QingAppfieldsMapper qingAppfieldsMapper;

    @Resource
    private RealtimeMapper realtimeMapper;

    @Resource(name = "redisTemplateLinux")
    private RedisTemplate<String,Object> redisTemplateLinux;


    public void addAbnormal(String appKey,Long eId,String wmMsg,String wmTime,String key) throws Exception {
        QueryWrapper<QingAppfieldsEntity> qingAppfieldsEntityQueryWrapper = new QueryWrapper<>();
        qingAppfieldsEntityQueryWrapper.eq("appKey", appKey);
        List<QingAppfieldsEntity> qingAppfieldsEntities = qingAppfieldsMapper.selectList(qingAppfieldsEntityQueryWrapper);

        List<Map<String, Object>> answers = new ArrayList<>();
        for (QingAppfieldsEntity qingAppfieldsEntity : qingAppfieldsEntities) {

            Map<String,Object> answer=new HashMap<>();
            List<Map<String, Object>> values = new ArrayList<>();
            Map<String, Object> value = new HashMap<>();
            switch (qingAppfieldsEntity.getQueTitle()) {
                case "设备编号": {
                    value=loadValue(qingAppfieldsEntity.getQueId(),eId);
                    values.add(value);
                    answer = loadAnswer(qingAppfieldsEntity.getQueId(), qingAppfieldsEntity.getQueTitle(), qingAppfieldsEntity.getQueType(), values);
                    answers.add(answer);
                    break;
                }
                case "异常停机原因": {
                    value=loadValue(qingAppfieldsEntity.getQueId(),wmMsg);
                    values.add(value);
                    answer = loadAnswer(qingAppfieldsEntity.getQueId(), qingAppfieldsEntity.getQueTitle(), qingAppfieldsEntity.getQueType(), values);
                    answers.add(answer);
                    break;
                }
                case "异常停机时间": {
                    value=loadValue(qingAppfieldsEntity.getQueId(),wmTime);
                    values.add(value);
                    answer = loadAnswer(qingAppfieldsEntity.getQueId(), qingAppfieldsEntity.getQueTitle(), qingAppfieldsEntity.getQueType(), values);
                    answers.add(answer);
                    break;
                }
            }

        }

        QingAddDataUserModel qingAddDataUserModel = new QingAddDataUserModel();
        qingAddDataUserModel.setEmail(email);
        Map<String, Object> body = new HashMap<>();
        body.put("applyUser",qingAddDataUserModel);
        body.put("answers",answers);
        String jsonBody = JSONUtil.toJsonStr(body);

        QingAddDataResultMain qingAddDataResultMain = QingflowAPIUtils.AddDataToQing(qingflowApiInfo, appKey, jsonBody);
        if(qingAddDataResultMain.getErrCode()==0){
            QingQueryActionResultBase qingQueryActionResultBase = QingflowAPIUtils.GetRequestResult(qingflowApiInfo, qingAddDataResultMain.getResult().getRequestId());
            if(qingQueryActionResultBase.getErrorCode()==0){
//                QueryWrapper<RealtimeEntity> queryWrapper=new QueryWrapper<>();
//                queryWrapper.eq("e_id", eId);
//                RealtimeEntity realtimeEntity = realtimeMapper.selectOne(queryWrapper);
//                realtimeEntity.setId(eId);
//                realtimeEntity.setAddStatus(2);
//                realtimeMapper.updateById(realtimeEntity);
                Boolean delete = redisTemplateLinux.delete(key);
                //System.out.println("异常"+delete);
            }
        }

    }

    public void addStop(String appKey,Long eId,String lastSpcTime,String key) throws Exception {
        QueryWrapper<QingAppfieldsEntity> qingAppfieldsEntityQueryWrapper = new QueryWrapper<>();
        qingAppfieldsEntityQueryWrapper.eq("appKey", appKey);
        List<QingAppfieldsEntity> qingAppfieldsEntities = qingAppfieldsMapper.selectList(qingAppfieldsEntityQueryWrapper);

        List<Map<String, Object>> answers = new ArrayList<>();
        for (QingAppfieldsEntity qingAppfieldsEntity : qingAppfieldsEntities) {

            Map<String,Object> answer=new HashMap<>();
            List<Map<String, Object>> values = new ArrayList<>();
            Map<String, Object> value = new HashMap<>();
            switch (qingAppfieldsEntity.getQueTitle()) {
                case "设备编号": {
                    value=loadValue(qingAppfieldsEntity.getQueId(),eId);
                    values.add(value);
                    answer = loadAnswer(qingAppfieldsEntity.getQueId(), qingAppfieldsEntity.getQueTitle(), qingAppfieldsEntity.getQueType(), values);
                    answers.add(answer);
                    break;
                }
                case "停机时间": {
                    value=loadValue(qingAppfieldsEntity.getQueId(),lastSpcTime);
                    values.add(value);
                    answer = loadAnswer(qingAppfieldsEntity.getQueId(), qingAppfieldsEntity.getQueTitle(), qingAppfieldsEntity.getQueType(), values);
                    answers.add(answer);
                    break;
                }
            }

        }

        QingAddDataUserModel qingAddDataUserModel = new QingAddDataUserModel();
        qingAddDataUserModel.setEmail(email);
        Map<String, Object> body = new HashMap<>();
        body.put("applyUser",qingAddDataUserModel);
        body.put("answers",answers);
        String jsonBody = JSONUtil.toJsonStr(body);

        QingAddDataResultMain qingAddDataResultMain = QingflowAPIUtils.AddDataToQing(qingflowApiInfo, appKey, jsonBody);
        if(qingAddDataResultMain.getErrCode()==0){
            QingQueryActionResultBase qingQueryActionResultBase = QingflowAPIUtils.GetRequestResult(qingflowApiInfo, qingAddDataResultMain.getResult().getRequestId());
            if(qingQueryActionResultBase.getErrorCode()==0){
//                QueryWrapper<RealtimeEntity> queryWrapper=new QueryWrapper<>();
//                queryWrapper.eq("e_id", eId);
//                RealtimeEntity realtimeEntity = realtimeMapper.selectOne(queryWrapper);
//                realtimeEntity.setId(eId);
//                realtimeEntity.setAddStatus(2);
//                realtimeMapper.updateById(realtimeEntity);
                Boolean delete = redisTemplateLinux.delete(key);
                //System.out.println("停机"+delete);
            }
        }

    }

//    public boolean updateEqNum(String appKey,Long applyId,Integer gatherCloseNum,Integer openAndCloseNum) throws Exception {
//        QueryWrapper<QingAppfieldsEntity> qingAppfieldsEntityQueryWrapper = new QueryWrapper<>();
//        qingAppfieldsEntityQueryWrapper.eq("appKey", appKey);
//        List<QingAppfieldsEntity> qingAppfieldsEntities = qingAppfieldsMapper.selectList(qingAppfieldsEntityQueryWrapper);
//
//        List<Map<String, Object>> answers = new ArrayList<>();
//        for (QingAppfieldsEntity qingAppfieldsEntity : qingAppfieldsEntities) {
//
//            Map<String,Object> answer=new HashMap<>();
//            List<Map<String, Object>> values = new ArrayList<>();
//            Map<String, Object> value = new HashMap<>();
//            switch (qingAppfieldsEntity.getQueTitle()) {
//                case "采集合模数": {
//                    value=loadValue(qingAppfieldsEntity.getQueId(),gatherCloseNum);
//                    values.add(value);
//                    answer = loadAnswer(qingAppfieldsEntity.getQueId(), qingAppfieldsEntity.getQueTitle(), qingAppfieldsEntity.getQueType(), values);
//                    answers.add(answer);
//                    break;
//                }
//                case "开合模次数": {
//                    value=loadValue(qingAppfieldsEntity.getQueId(),openAndCloseNum);
//                    values.add(value);
//                    answer = loadAnswer(qingAppfieldsEntity.getQueId(), qingAppfieldsEntity.getQueTitle(), qingAppfieldsEntity.getQueType(), values);
//                    answers.add(answer);
//                    break;
//                }
//            }
//
//        }
//
//        Map<String, Object> body = new HashMap<>();
//        body.put("answers",answers);
//        String jsonBody = JSONUtil.toJsonStr(body);
//
//        QingAddDataResultMain qingAddDataResultMain = QingflowAPIUtils.UpdDataToQing(qingflowApiInfo, appKey, jsonBody, String.valueOf(applyId));
//        boolean flag=false;
//        if(qingAddDataResultMain.getErrCode()==0) {
//            QingQueryActionResultBase qingQueryActionResultBase = QingflowAPIUtils.GetRequestResult(qingflowApiInfo, qingAddDataResultMain.getResult().getRequestId());
//            if (qingQueryActionResultBase.getErrorCode() == 0) {
//                flag=true;
//            }
//        }
//        return flag;
//
//    }

    private Map<String,Object> loadAnswer(Long queId,String queTitle,int queType,List<Map<String, Object>> values){
        Map<String,Object> answer=new HashMap<>();
        answer.put("queId", queId);
        answer.put("queTitle", queTitle);
        answer.put("queType", queType);
        answer.put("values", values);
        
        return answer;
    }

    private Map<String,Object> loadValue(Long queId,Object valueStr){
        Map<String,Object> value=new HashMap<>();
        value.put("queId", queId);
        value.put("value", valueStr);

        return value;
    }

}
